//
//  WBHomeOriginalView.swift
//  WB123
//
//  Created by xuyunde mac pro on 2017/7/4.
//  Copyright © 2017年 NSObjiect. All rights reserved.
//

import UIKit
import SnapKit
class WBHomeOriginalView: UIView {

    // 记录原创微博的底部约束
    var originalViewBottomConstraint: Constraint?
    //提供一个模型属性,供外界赋值
    var statusViewModel: WBStatusViewModel?{
        didSet{
            // 赋值
            
            headImageView.wb_setImage(withUrlString: statusViewModel?.homeModel?.user?.profile_image_url, placeholderImgName: "avatar_default_big")
            nameLabel.text = statusViewModel?.homeModel?.user?.name
            verifiedImageView.image = statusViewModel?.verifiedImage
            mbrankImageView.image = statusViewModel?.mbrankImage
            contentLabel.text = statusViewModel?.homeModel?.text
            /*
             - 卸载riginalView.bottom
             - 判断是否有配图
             - 判断条件statusViewModel?.homeModel?.pic_urls?.count > 0
             - 如果count > 0 有配图
             - 赋值
             - originalView.bottom = pictureView.bottom + 10
             - 显示配图
             - 反之 没有配图
             - originalView.bottom = contentLabel.bottom + 10
             - 隐藏配图
             
             */
            // 卸载约束
            originalViewBottomConstraint?.deactivate()
            // 判断是否有视图
            if let picUrls = statusViewModel?.homeModel?.pic_urls, picUrls.count > 0{
                // 赋值
                pictureView.picUrlsList = picUrls
                
                snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                //显示视图
                pictureView.isHidden = false
                
            }else{
                // 没有配图
                snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                pictureView.isHidden = true
            }
            // 微博时间
            timeLabel.text = statusViewModel?.createdAtText
            // 来源
            sourceLabel.text = statusViewModel?.sourceText
        }
    }
       
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    private func setupUI(){
        backgroundColor = UIColor.white
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        //添加约束
        headImageView.snp.makeConstraints { (make) in
            make.top.left.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.left.equalTo(headImageView.snp.right).offset(10)
        }
        
        mbrankImageView.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.right).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp.right).offset(10)
        }
        
        verifiedImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp.right)
            make.centerY.equalTo(headImageView.snp.bottom)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headImageView)
            make.top.equalTo(headImageView.snp.bottom).offset(10)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        snp.makeConstraints { (make) in
            originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        
    }
    //懒加载控件
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(named:"avatar_default_big")
    // 昵称
    private lazy var nameLabel: UILabel = UILabel(text: "昵称", textColor: UIColor.black, fontSize: NormalFontSize)
    // 会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(named: "common_icon_membership")
    // 时间
    private lazy var timeLabel: UILabel = UILabel(text: "刚刚", textColor: ThemeColor, fontSize: SmallFontSize)
    // 来源
    private lazy var sourceLabel: UILabel = UILabel(text: "来自 黑马25期微博", textColor: ThemeColor, fontSize: SmallFontSize)
    // 认证
    private lazy var verifiedImageView: UIImageView = UIImageView(named: "avatar_vgirl")
    // 微博正文
    private lazy var contentLabel: UILabel = UILabel(text: "", textColor: UIColor.black, fontSize: NormalFontSize, maxWidth: SCREENW - 20)
   // 配图
    private lazy var pictureView: WBHomePictureView = {
        let view = WBHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
}
